//
//  HPSendSignViewController.m
//  HPZZTong
//
//  Created by handpay on 14-4-2.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPSendSignViewController.h"

#define phoneHolder   @"请填写顾客手机号"

@interface HPSendSignViewController ()

- (IBAction)cancelClick:(id)sender;
- (IBAction)okClick:(id)sender;

@end

@implementation HPSendSignViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIView* bgImageView = [self.view viewWithTag:bgImageViewTag];
    if (bgImageView) {
        bgImageView.frame = CGRectMake(0, 0, ScreenHeight, ScreenWidth);
    }
    
    if (HP_Device_iPhone5) {
        int left = 40;
        CGRect cancelFrame = self.cancelButton.frame;
        cancelFrame.origin.x = left;
        self.cancelButton.frame = cancelFrame;
        
        CGRect frame = self.okButton.frame;
        frame.origin.x = left * 2 + cancelFrame.size.width;
        self.okButton.frame = frame;
    }
    self.oneButton.textStr = @"1";
    self.twoButton.textStr = @"2";
    self.threeButton.textStr = @"3";
    self.fourButton.textStr = @"4";
    self.fiveButton.textStr = @"5";
    self.sixButton.textStr = @"6";
    self.sevenButton.textStr = @"7";
    self.eightButton.textStr = @"8";
    self.nineButton.textStr = @"9";
    self.zeroButton.textStr = @"0";
    
    [self.leftBgView layer].cornerRadius = 5.0;
    [self.leftBgView layer].borderColor = kLineFontColor.CGColor;
    [self.leftBgView layer].borderWidth = 1;
    
    [self.phoneLabel layer].cornerRadius = 8.0;
    [self.phoneLabel layer].borderColor = kLineFontColor.CGColor;
    [self.phoneLabel layer].borderWidth = 1;
    
    if (kVerChannel == VerChannelHeXin) {
        [self.okButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
        
        [self.okButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
    }else{
        [self.okButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton2.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
        
        [self.okButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover2.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
    }
    [self.cancelButton setBackgroundImage:[[UIImage imageNamed:@"yellowbutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.cancelButton setBackgroundImage:[[UIImage imageNamed:@"yellowbuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];


}
//- (BOOL)shouldAutorotate{
//    return NO;
//}
//
//-(NSUInteger)supportedInterfaceOrientations{
//    return UIInterfaceOrientationMaskLandscapeRight;
//}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}
- (void)transitionView:(BOOL)isVeri{
    [[UIApplication sharedApplication] setStatusBarHidden:isVeri];
    [[ZZT_UIENG getCurrentNavigation] setNavigationBarHidden:isVeri];
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    [ZZT_UIENG getCurrentNavigation].view.transform = CGAffineTransformIdentity;
    if (isVeri) {
        
        [ZZT_UIENG getCurrentNavigation].view.transform = CGAffineTransformMakeRotation(M_PI*(90)/180.0);
        [ZZT_UIENG getCurrentNavigation].view.bounds = CGRectMake(0, 0, ScreenHeight, ScreenWidth);
        [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
    }else{
        [ZZT_UIENG getCurrentNavigation].view.transform = CGAffineTransformMakeRotation(M_PI*(0)/180.0);
        [ZZT_UIENG getCurrentNavigation].view.bounds = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait animated:NO];
    }
    
    [UIView commitAnimations];
    [[ZZT_UIENG getCurrentNavigation] setNavigationBarHidden:isVeri];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self transitionView:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self transitionView:NO];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_leftBgView release];
    [_phoneLabel release];
    [_cancelButton release];
    [_okButton release];
    [_delButton release];
    [_oneButton release];
    [_twoButton release];
    [_threeButton release];
    [_fourButton release];
    [_fiveButton release];
    [_sixButton release];
    [_sevenButton release];
    [_eightButton release];
    [_nineButton release];
    [_zeroButton release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setLeftBgView:nil];
    [self setPhoneLabel:nil];
    [self setCancelButton:nil];
    [self setOkButton:nil];
    [self setDelButton:nil];
    [self setOneButton:nil];
    [self setTwoButton:nil];
    [self setThreeButton:nil];
    [self setFourButton:nil];
    [self setFiveButton:nil];
    [self setSixButton:nil];
    [self setSevenButton:nil];
    [self setEightButton:nil];
    [self setNineButton:nil];
    [self setZeroButton:nil];
    [super viewDidUnload];
}
- (IBAction)numberClick:(id)sender {
    HPCustomButton* button = (HPCustomButton*)sender;
    NSString* phoneStr = self.phoneLabel.text;
    
    if ([phoneStr isEqualToString:phoneHolder]) {
        phoneStr = button.textStr;
    }else{
        if(phoneStr.length >= 11){
            return;
        }
        phoneStr = [phoneStr stringByAppendingString:button.textStr];
    }
    self.phoneLabel.textColor = [UIColor blackColor];
    self.phoneLabel.text = phoneStr;
}

- (IBAction)delButtonClick:(id)sender {
    
    NSString* phoneStr = self.phoneLabel.text;
    if ([phoneStr isEqualToString:phoneHolder]) {
        return;
    }
    phoneStr = [phoneStr substringToIndex:(phoneStr.length - 1)];
    
    if ([NSString isNilOrEmpty:phoneStr]) {
        phoneStr = phoneHolder;
        self.phoneLabel.textColor = kGrayFontColor;
    }
    self.phoneLabel.text = phoneStr;
}
- (IBAction)cancelClick:(id)sender {
    CallbackData* callback_ = [self.screenData objectForKey:KEY_CALLBACK];
    LOG(@"send sign cancelclick:%@",callback_.target);
    UIImage* signImage = [self.screenData objectForKey:@"signImage"];
    
//    [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait animated:NO];
    
    if (callback_ && [callback_.target respondsToSelector:callback_.callback]) {
        [callback_.target performSelector:callback_.callback withObject:signImage withObject:nil];
    }
    [HPUIENG popScreenByNum:2];
}

- (IBAction)okClick:(id)sender {
    NSString* text = self.phoneLabel.text;
    if ([NSString isNilOrEmpty:text]) {
        [HPUIENG showAlert:@"请输入手机号码"];
        return;
    }
    NSString* phoneSub = [text substringToIndex:1];
    if (text.length < 11 || text.length > 11 || (phoneSub && ![phoneSub isEqualToString:@"1"])) {
        [HPUIENG showAlert:@"请输入正确的手机号码"];
        return;
    }
    CallbackData* callback_ = [self.screenData objectForKey:KEY_CALLBACK];
    LOG(@"sendsign callback okclick:%@",callback_.target);
    UIImage* signImage = [self.screenData objectForKey:@"signImage"];
    
//    [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait animated:NO];
    
    if (callback_ && [callback_.target respondsToSelector:callback_.callback]) {
        [callback_.target performSelector:callback_.callback withObject:signImage withObject:text];
    }
    [HPUIENG popScreenByNum:2];

}
@end
